package com.odriver.neulearn.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.odriver.neulearn.Adapter.MenuListviewAdapter;
import com.odriver.neulearn.Bean.MenuBean;
import com.odriver.neulearn.R;
import com.odriver.neulearn.Util.SaveUtil;
import com.odriver.neulearn.Util.ScreenUtil;
import com.odriver.neulearn.Util.TransferUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MeunActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private MenuBean menuBean = null;
    private static RequestQueue mQueue;
    private ListView listView;
    private MenuListviewAdapter adapter;
    private String MENU_URL = "http://www.neuedu.cn/m/mobilePostInfo!findPostInfo.action";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtil.setScreenNoTitle(this);
        setContentView(R.layout.activity_meun);
        initData();
        initView();
    }

    public void initData() {
        mQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, MENU_URL,
                new Response.Listener() {
                    @Override
                    public void onResponse(Object response) {
                        Gson gson = new Gson();
                        menuBean = gson.fromJson((String) response, MenuBean.class);
                        //Log.d("@@@@", menuBean.toString());
                        initView();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MeunActivity.this, "下载列表信息错误", Toast.LENGTH_SHORT).show();
                        Log.e("网络消息", error.toString());
                    }

                }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("Cookie", SaveUtil.getCookie(MeunActivity.this));
                return map;
            }
        };
        mQueue.add(stringRequest);
    }


    public void initView() {
        listView = (ListView) findViewById(R.id.idp_menu_listview);

        if (menuBean == null) {
            adapter = new MenuListviewAdapter(this, null);
            listView.setAdapter(adapter);
        } else {
            listView.setAdapter(adapter);
            adapter.updateData(menuBean.getIndexPositionList());
            listView.setOnItemClickListener(this);
        }

    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (menuBean == null) {
            return;
        }
        Intent intent = new Intent(MeunActivity.this, CourseDetail.class);
        Bundle bundle = new Bundle();
        List<MenuBean.IndexPositionListBean> indexPositionListBean = menuBean.getIndexPositionList();
        MenuBean.IndexPositionListBean dataBean = indexPositionListBean.get(i);
        bundle.putString("url",dataBean.getPostUrl());
        bundle.putInt("courseID", dataBean.getPostId());
        bundle.putString("topic", dataBean.getPostName());
        bundle.putInt("courseNum", dataBean.getCourseNums());
        bundle.putInt("courseTime", dataBean.getCourseHours());
        bundle.putInt("video", dataBean.getVideoNums());
        bundle.putInt("test", dataBean.getTestNums());
        bundle.putInt("courseDay", dataBean.getCourseHours());
        bundle.putInt("people", dataBean.getPersonNums());
        bundle.putString("detail", dataBean.getPostDesc().replace("&nbsp;", " "));
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
